package com.sx.zongzhi.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.sx.zongzhi.BaseActivity;
import com.sx.zongzhi.R;
import com.sx.zongzhi.adapter.MyFragmentPagerAdapter;
import com.sx.zongzhi.bean.MsgEvent;
import com.sx.zongzhi.bean.PhotoBean;
import com.sx.zongzhi.bean.PopulationFixedDetailBean;
import com.sx.zongzhi.fragment.FixedBasicInforEditFragment;
import com.sx.zongzhi.fragment.FixedBusinessEditFragment;
import com.sx.zongzhi.presenter.PopFixedAddPest;
import com.sx.zongzhi.presenter.UploadFilePest;
import com.sx.zongzhi.utils.BaseUtil;
import com.sx.zongzhi.utils.MyRxBus;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.IPopFixedAddView;
import com.sx.zongzhi.view.UploadFileView;
import com.sx.zongzhi.widget.LoadDialog;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 添加/编辑留守人口
 */
public class AddFixedActivity extends BaseActivity implements IPopFixedAddView,UploadFileView{

    @BindView(R.id.tl_TabLayout)
    TabLayout tl_TabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    private List<Fragment> fragments;

    private PopulationFixedDetailBean detailBean;
    private String orgId="";
    private FixedBasicInforEditFragment basicInforEditFragment;
    private FixedBusinessEditFragment businessEditFragment;
    private String path="";
    private boolean isEdit;

    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_household_detail);
        setTitleName("留守人口新增");
        ButterKnife.bind(this);
        setTitleRight(R.string.submit);
        orgId=getIntent().getStringExtra("orgId");
        detailBean= (PopulationFixedDetailBean) getIntent().getSerializableExtra("PopulationFixedDetailBean");
        isEdit= getIntent().getBooleanExtra("isEdit",false);
        initView();
    }
    @OnClick({R.id.tv_right})
    public void OnClick(View view){
        switch (view.getId()){
            case R.id.tv_right:
                submit();
                break;
        }
    }
    public void submit(){
        boolean basicDetail=basicInforEditFragment.submit();
        if (!basicDetail){
            return;
        }
        boolean businessDetail=businessEditFragment.submit();
        if (!businessDetail){
            return;
        }
        if(BaseUtil.isEmpty(detailBean.getImgUrl())|| detailBean.getImgUrl().startsWith("http")){
            submitDone();
        }else {
            List<File> files=new ArrayList<>();
            files.add(new File(detailBean.getImgUrl()));
            LoadDialog.show(this,"正在上传头像中...");
            new UploadFilePest(this).uploadFile(this,files);
        }


    }

    private void submitDone() {
        PopFixedAddPest addPest=new PopFixedAddPest(this);
        LoadDialog.show(this,"正在提交");
        if (isEdit){
            addPest.addFixed("updateLeftBehindPeople.action",orgId,detailBean.getId(),path,detailBean);
        }else {
            addPest.addFixed("addLeftBehindPeople.action",orgId,"",path,detailBean);
        }
    }

    private void initView() {
        if (isEdit){
            setTitleName("留守人口修改");
        }else {
            setTitleName("留守人口新增");
        }
        List<String> titles=new ArrayList<>();
        titles.add("基本信息");
        titles.add("业务信息");

        if (detailBean==null){
            detailBean=new PopulationFixedDetailBean();
        }
        Bundle bundle=new Bundle();
        bundle.putSerializable("PopulationFixedDetailBean",detailBean);
        bundle.putBoolean("isEdit",isEdit);
        bundle.putString("orgId",orgId);
        basicInforEditFragment=new FixedBasicInforEditFragment();
        basicInforEditFragment.setArguments(bundle);
        businessEditFragment=new FixedBusinessEditFragment();
        businessEditFragment.setArguments(bundle);
        fragments=new ArrayList<>();
        fragments.add(basicInforEditFragment);
        fragments.add(businessEditFragment);
        tl_TabLayout.setTabMode(TabLayout.MODE_FIXED);
        //为TabLayout添加tab名称
        tl_TabLayout.addTab(tl_TabLayout.newTab().setText(titles.get(0)));
        tl_TabLayout.addTab(tl_TabLayout.newTab().setText(titles.get(1)));
        MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragments, titles);
        viewPager.setAdapter(pagerAdapter);
        viewPager.setOffscreenPageLimit(2);
        //tabLayout加载viewpager
        tl_TabLayout.setupWithViewPager(viewPager);


    }

    @Override
    public void addSuccess(Boolean result) {
        LoadDialog.dismiss(this);
        if (result){
            MyRxBus.get().post(new MsgEvent<>("OverseasActivity",2,"成功"));
            ToastUtils.showToast(this,"保存成功");
            finish();
        }
    }

    @Override
    public void showError(String message) {
        LoadDialog.dismiss(this);
        ToastUtils.showToast(this,message);
    }

    @Override
    public void uploadSuccess(List<PhotoBean> beans) {
        if(beans!=null&&beans.size()>0){
            LoadDialog.show(this,"上传成功");
            path=beans.get(0).getPath();
            submitDone();
        }else {
            LoadDialog.dismiss(this);
        }
    }
}
